package com.yc.UI;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.layout.FillLayout;

import java.io.ByteArrayInputStream;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.dao.UserDao;
import com.yc.utils.Values;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

/**
 *用户信息面板
 * @author UNO
 * 2021年6月24日下午6:56:43
 */
public class UserInfoUI extends Composite {
	//获取到选择登录的用户的信息
	private Map<String, Object> map = Values.currentLoginUser;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public UserInfoUI(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		composite.setBackgroundMode(SWT.INHERIT_FORCE);

		//图片框
		Label Label_photo = new Label(composite, SWT.NONE);
		Label_photo.setLocation(10, 9);
		Label_photo.setSize(150, 150);
		//获得用户信息map里面的图片 存入一个Object对象里面
		Object obj = map.get("u_photo");
		if(obj == null) { //说明用户没有设置头像, 给他一个默认头像
			Label_photo.setImage(SWTResourceManager.getImage(UserInfoUI.class, "/images/默认头像.png"));
		}else {
			//把图片由Object类型转换成字节数组
			byte[] bt = (byte[]) obj;
			//把字节数组类型的图片 通过数据流 存入(转换成) imageData中
			ImageData imageData = new ImageData(new ByteArrayInputStream(bt));
			//图片自适应
			imageData = imageData.scaledTo(Label_photo.getSize().x, Label_photo.getSize().y);
			Image image = new Image(Display.getDefault(), imageData);
			Label_photo.setImage(image);
		}

		Label label1 = new Label(composite, SWT.NONE);
		label1.setLocation(170, 10);
		label1.setSize(66, 28);
		label1.setText("姓名");
		//用户姓名
		Label Label_1 = new Label(composite, SWT.NONE);
		Label_1.setBounds(296, 10, 113, 20);
		String account = map.get("u_name").toString();
		Label_1.setText(account);

		Label Label2 = new Label(composite, SWT.NONE);
		Label2.setBounds(171, 65, 76, 20);
		Label2.setText("性别");
		//用户性别
		Label Label_2 = new Label(composite, SWT.NONE);
		Label_2.setBounds(296, 65, 60, 20);
		String sex = map.get("u_sex").toString();
		Label_2.setText(sex);

		Label Label3 = new Label(composite, SWT.NONE);
		Label3.setBounds(473, 65, 76, 20);
		Label3.setText("余额");
		//用户余额
		Label Label_3 = new Label(composite, SWT.CENTER);
		Label_3.setBounds(562, 65, 76, 20);
		String balance = map.get("u_balance").toString();
		Label_3.setText(balance);

		Label Label4 = new Label(composite, SWT.NONE);
		Label4.setBounds(166, 128, 76, 20);
		Label4.setText("身份证");
		//用户身份证号码
		Label Label_4 = new Label(composite, SWT.NONE);
		Label_4.setBounds(296, 127, 232, 20);
		String cardId = map.get("u_cardid").toString();
		Label_4.setText(cardId);

		Label Label5 = new Label(composite, SWT.NONE);
		Label5.setBounds(45, 234, 76, 20);
		Label5.setText("密码");
		//用户密码
		Label Label_5 = new Label(composite, SWT.NONE);
		Label_5.setBounds(142, 234, 179, 20);
		String pwd = map.get("u_pwd").toString();
		Label_5.setText(pwd);

		Label Label6 = new Label(composite, SWT.NONE);
		Label6.setBounds(45, 307, 76, 20);
		Label6.setText("手机号");
		//用户绑定的手机号
		Label Label_6 = new Label(composite, SWT.NONE);
		Label_6.setBounds(142, 307, 179, 20);
		String tel = map.get("u_tel").toString();
		Label_6.setText(tel);

		Label Label7 = new Label(composite, SWT.NONE);
		Label7.setBounds(45, 370, 76, 20);
		Label7.setText("邮箱");
		//用户绑定的邮箱
		Label Label_7 = new Label(composite, SWT.NONE);
		Label_7.setBounds(142, 370, 179, 20);
		String email = map.get("u_email").toString();
		Label_7.setText(email);

		Label label_addMamey = new Label(composite, SWT.CENTER);

		label_addMamey.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_addMamey.setBounds(658, 432, 86, 41);
		label_addMamey.setText("充值");
		label_addMamey.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));

		Label label_update = new Label(composite, SWT.CENTER);
		label_update.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_update.setBounds(525, 432, 106, 41);
		label_update.setText("修改信息");
		label_update.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));

		Label label_update_1 = new Label(composite, SWT.CENTER);

		label_update_1.setText("刷新");
		label_update_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
		label_update_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_update_1.setBounds(398, 432, 106, 41);

		//点击修改信息，进入修改信息界面
		label_update.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				UserUpDateInfo ud = new UserUpDateInfo();
				ud.open();
			}
		});
		//点击进入充值界面
		label_addMamey.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				AddBalance ab = new AddBalance();
				ab.open();
			}
		});

		//点击刷新
		label_update_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				String name = map.get("u_name").toString();
				String password = map.get("u_pwd").toString();				
				UserDao userdao = new UserDao();
				Map<String,Object> map1 = userdao.findUserInfo(name, password);
				
				//传入默认头像
				Object obj1 = map1.get("u_photo");
				if(obj1 == null) { //说明用户没有设置头像, 给他一个默认头像
					return;			
				}else {
					//把图片由Object类型转换成字节数组
					byte[] bt = (byte[]) obj1;
					//把字节数组类型的图片 通过数据流 存入(转换成) imageData中
					ImageData imageData = new ImageData(new ByteArrayInputStream(bt));
					//图片自适应
					imageData = imageData.scaledTo(Label_photo.getSize().x, Label_photo.getSize().y);
					Image image = new Image(Display.getDefault(), imageData);
					Label_photo.setImage(image);
				}

				String account1 = map1.get("u_name").toString();
				String balance1 = map1.get("u_balance").toString();			
				String pwd1 = map1.get("u_pwd").toString();
				String tel1 = map1.get("u_tel").toString();
				String email1 = map1.get("u_email").toString();

				Label_1.setText(account1);
				Label_3.setText(balance1);
				Label_5.setText(pwd1);
				Label_6.setText(tel1);
				Label_7.setText(email1);
				//如果取到值，说明登录成功  map里存的是该用户所有的信息
				Values.currentLoginUser = map1;
			}
		});
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
